﻿// LTDMC_AxisControl_Dlg.cpp: 实现文件
//

#include "pch.h"
#include "SakerProject.h"
#include "LTDMC_AxisControl_Dlg.h"
#include "afxdialogex.h"

#include "xxFileProcess.h"
#include "xxWidgetControl.h"

#include "mmFile_Paras.h"

#include "Project.h"


// CLTDMC_AxisControl_Dlg 对话框
CLTDMC_AxisControl_Dlg * m_LTDMC_AxisControl;

IMPLEMENT_DYNAMIC(CLTDMC_AxisControl_Dlg, CDialogEx)

CLTDMC_AxisControl_Dlg::CLTDMC_AxisControl_Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LTDMC_AxisControl, pParent)
{

}

CLTDMC_AxisControl_Dlg::~CLTDMC_AxisControl_Dlg()
{
}

void CLTDMC_AxisControl_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CLTDMC_AxisControl_Dlg, CDialogEx)
	ON_WM_CTLCOLOR()
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_COMBO_AxisName, &CLTDMC_AxisControl_Dlg::OnCbnSelchangeComboAxisname)
	ON_BN_CLICKED(IDC_BUTTON_Enable, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonEnable)
	ON_BN_CLICKED(IDC_BUTTON_Disable, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonDisable)
	ON_BN_CLICKED(IDC_BUTTON_EnableAll, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonEnableall)
	ON_BN_CLICKED(IDC_BUTTON_DisableAll, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonDisableall)
	ON_BN_CLICKED(IDC_BUTTON_MoveP, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonMovep)
	ON_BN_CLICKED(IDC_BUTTON_MoveN, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonMoven)
	ON_BN_CLICKED(IDC_BUTTON_ResetPos, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonResetpos)
	ON_BN_CLICKED(IDC_BUTTON_EmergStop, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonEmergstop)
	ON_BN_CLICKED(IDC_BUTTON_Home, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonHome)
	ON_BN_CLICKED(IDC_BUTTON_Save, &CLTDMC_AxisControl_Dlg::OnBnClickedButtonSave)
END_MESSAGE_MAP()


// CLTDMC_AxisControl_Dlg 消息处理程序


BOOL CLTDMC_AxisControl_Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	xx::InitCB(this, IDC_COMBO_AxisName, mmLTDMC.vecAxisNames, CurAxis);
	InitCurAxis();

	SetTimer(Project.nTimer_LTDMC_AxisControl, 100, NULL);

	return TRUE;
}


void CLTDMC_AxisControl_Dlg::OnCancel()
{
	DeleteObject((HGDIOBJ)hbr);
	KillTimer(Project.nTimer_LTDMC_AxisControl);

	CDialogEx::OnCancel();
}


HBRUSH CLTDMC_AxisControl_Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	if (hbr != nullptr) {
		DeleteObject((HGDIOBJ)hbr);
	}
	switch (pWnd->GetDlgCtrlID())
	{
	case IDC_STATIC_Servo:
		hbr = (mmLTDMC.Axis[CurAxis].Servo.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_RED); break;
	case IDC_STATIC_Alarm:
		hbr = (mmLTDMC.Axis[CurAxis].Alarm.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_LimitP:
		hbr = (mmLTDMC.Axis[CurAxis].LimitP.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_LimitN:
		hbr = (mmLTDMC.Axis[CurAxis].LimitN.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_Emerg:
		hbr = (mmLTDMC.Axis[CurAxis].Emerg.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_LimitO:
		hbr = (mmLTDMC.Axis[CurAxis].LimitO.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_SoftP:
		hbr = (mmLTDMC.Axis[CurAxis].SoftP.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_SoftN:
		hbr = (mmLTDMC.Axis[CurAxis].SoftN.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_InPos:
		hbr = (mmLTDMC.Axis[CurAxis].InPos.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_RED); break;
	case IDC_STATIC_EZ:
		hbr = (mmLTDMC.Axis[CurAxis].EZ.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_Ready:
		hbr = (mmLTDMC.Axis[CurAxis].Ready.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_RED); break;
	case IDC_STATIC_DSTP:
		hbr = (mmLTDMC.Axis[CurAxis].DSTP.CheckTrig()) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	default:
		hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
		break;
	}
	pDC->SetBkMode(TRANSPARENT);
	return hbr;
}


void CLTDMC_AxisControl_Dlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == Project.nTimer_LTDMC_AxisControl) {
		SetDlgItemText(IDC_STATIC_Servo, L"使能");
		SetDlgItemText(IDC_STATIC_Alarm, L"报警");
		SetDlgItemText(IDC_STATIC_LimitP, L"极限 +");
		SetDlgItemText(IDC_STATIC_LimitN, L"极限 -");
		SetDlgItemText(IDC_STATIC_Emerg, L"急停");
		SetDlgItemText(IDC_STATIC_LimitO, L"原点");

		SetDlgItemText(IDC_STATIC_SoftP, L"软限位 +");
		SetDlgItemText(IDC_STATIC_SoftN, L"软限位 -");
		SetDlgItemText(IDC_STATIC_InPos, L"就位");
		SetDlgItemText(IDC_STATIC_EZ, L"EZ");
		SetDlgItemText(IDC_STATIC_Ready, L"准备");
		SetDlgItemText(IDC_STATIC_DSTP, L"减停");

		SetDlgItemInt(IDC_SHOW_CurPos, mmLTDMC.Axis[CurAxis].CurPos);
	}

	CDialogEx::OnTimer(nIDEvent);
}

void CLTDMC_AxisControl_Dlg::InitCurAxis() {
	CString iniField;
	iniField.Format(L"LTDMC_Axis_%02d... ...", CurAxis);
	CString iniCurrentPath = mmFile_Paras.iniModelPath();

	mmLTDMC.Axis[CurAxis].Flag_ServoMotor = xx::InitCK(this, IDC_CHECK_ServoMotor, iniField, L"ServoMotor", iniCurrentPath, 1);
	mmLTDMC.Axis[CurAxis].CardNo = xx::InitWidget(this, IDC_EDIT_CardNo, iniField, L"CardNo", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].AxisNo = xx::InitWidget(this, IDC_EDIT_AxisNo, iniField, L"AxisNo", iniCurrentPath, 0);

	mmLTDMC.Axis[CurAxis].InitSpeed = xx::InitWidget(this, IDC_EDIT_InitSpeed, iniField, L"InitSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].AccSpeed = xx::InitWidget(this, IDC_EDIT_AccSpeed, iniField, L"AccSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].DecSpeed = xx::InitWidget(this, IDC_EDIT_DecSpeed, iniField, L"DecSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].StopSpeed = xx::InitWidget(this, IDC_EDIT_StopSpeed, iniField, L"StopSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].MaxSpeed = xx::InitWidget(this, IDC_EDIT_MaxSpeed, iniField, L"MaxSpeed", iniCurrentPath, 0);

	mmLTDMC.Axis[CurAxis].HomeP = xx::InitCK(this, IDC_CHECK_HomeP, iniField, L"HomeP", iniCurrentPath);
	mmLTDMC.Axis[CurAxis].HomeSpeed = xx::InitWidget(this, IDC_EDIT_HomeSpeed, iniField, L"HomeSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].HomeOffset = xx::InitWidget(this, IDC_EDIT_HomeOffset, iniField, L"HomeOffset", iniCurrentPath, 0);
}

void CLTDMC_AxisControl_Dlg::OnCbnSelchangeComboAxisname()
{
	CurAxis = ((CComboBox*)GetDlgItem(IDC_COMBO_AxisName))->GetCurSel();
	InitCurAxis();
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonEnable()
{
	mmLTDMC.Axis[CurAxis].EnableServo(TRUE);
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonDisable()
{
	mmLTDMC.Axis[CurAxis].EnableServo(false);
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonEnableall()
{
	mmLTDMC.EnableAllServo(TRUE);
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonDisableall()
{
	mmLTDMC.EnableAllServo(false);
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonMovep()
{
	int MoveDistance = xx::ReadWidget(this, IDC_EDIT_MoveDistance, 0);
	mmLTDMC.Move(MoveDistance, CurAxis);
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonMoven()
{
	int MoveDistance = xx::ReadWidget(this, IDC_EDIT_MoveDistance, 0);
	mmLTDMC.Move(-MoveDistance, CurAxis);
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonResetpos()
{
	mmLTDMC.Axis[CurAxis].ResetPos();
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonEmergstop()
{
	mmLTDMC.Stop();
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonHome()
{
	mmLTDMC.Home({ CurAxis });
}


void CLTDMC_AxisControl_Dlg::OnBnClickedButtonSave()
{
	CString iniField;
	iniField.Format(L"LTDMC_Axis_%02d... ...", CurAxis);
	CString iniCurrentPath = mmFile_Paras.iniModelPath();

	mmLTDMC.Axis[CurAxis].Flag_ServoMotor = xx::DefaultCK(this, IDC_CHECK_ServoMotor, iniField, L"ServoMotor", iniCurrentPath);
	mmLTDMC.Axis[CurAxis].CardNo = xx::DefaultWidget(this, IDC_EDIT_CardNo, iniField, L"CardNo", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].AxisNo = xx::DefaultWidget(this, IDC_EDIT_AxisNo, iniField, L"AxisNo", iniCurrentPath, 0);

	mmLTDMC.Axis[CurAxis].InitSpeed = xx::DefaultWidget(this, IDC_EDIT_InitSpeed, iniField, L"InitSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].AccSpeed = xx::DefaultWidget(this, IDC_EDIT_AccSpeed, iniField, L"AccSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].DecSpeed = xx::DefaultWidget(this, IDC_EDIT_DecSpeed, iniField, L"DecSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].StopSpeed = xx::DefaultWidget(this, IDC_EDIT_StopSpeed, iniField, L"StopSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].MaxSpeed = xx::DefaultWidget(this, IDC_EDIT_MaxSpeed, iniField, L"MaxSpeed", iniCurrentPath, 0);

	mmLTDMC.Axis[CurAxis].HomeP = xx::DefaultCK(this, IDC_CHECK_HomeP, iniField, L"HomeP", iniCurrentPath);
	mmLTDMC.Axis[CurAxis].HomeSpeed = xx::DefaultWidget(this, IDC_EDIT_HomeSpeed, iniField, L"HomeSpeed", iniCurrentPath, 0);
	mmLTDMC.Axis[CurAxis].HomeOffset = xx::DefaultWidget(this, IDC_EDIT_HomeOffset, iniField, L"HomeOffset", iniCurrentPath, 0);

}
